package cn.school.mybatis.Util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringMButil {
    private static ApplicationContext applicationContext; // Spring应用上下文环境
    static{
        applicationContext=new ClassPathXmlApplicationContext("spring-mybatis.xml");
    }
    private SpringMButil(){

    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext)  {
        SpringMButil.applicationContext = applicationContext;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) applicationContext.getBean(name);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<?> clz) throws BeansException {
        return (T) applicationContext.getBean(clz);
    }

//    public final static SpringMButil SPRING_MB_UTIL =new SpringMButil();
//    ApplicationContext ac=new ClassPathXmlApplicationContext("spring-mybatis.xml");
//
//    public SqlSessionTemplate getSqlSessionTemplate(String str){
//        SqlSessionTemplate sqlSessionTemplate=(SqlSessionTemplate)ac.getBean(str);
////        SqlSessionTemplate sqlSessionTemplate=(SqlSessionTemplate)ac.getBean("sessionTemplate");
//        return sqlSessionTemplate;
//    }

}
